﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Miris.HumanMilkAnalyser.DataModel;
using Miris.HumanMilkAnalyser.Repository.Interface;

namespace Miris.HumanMilkAnalyser.Repository.EntityFramework
{
    /// <summary>
    /// A factory class that manage every single repository.
    /// </summary>
    public class RepositoryFactory : IRepositoryFactory
    {
        private MirisHumanMilkAnalyserContext _dataContext;

        /// <summary>
        /// Open a database connection without a transaction
        /// </summary>
        public void OpenSession()
        {
            _dataContext = new MirisHumanMilkAnalyserContext();
        }

        /// <summary>
        /// Open a database connection with a transaction
        /// </summary>
        public void OpenSessionWithTransaction()
        {
            OpenSession();
        }

        /// <summary>
        /// Rollback the transaction
        /// </summary>
        public void Rollback() { }

        /// <summary>
        /// Commit the transaction
        /// </summary>
        public void Commit()
        {
            if (_dataContext != null)
            {
                _dataContext.SaveChanges();
            }
        }

        /// <summary>
        /// Flush current data
        /// </summary>
        public void Flush() { }

        /// <summary>
        /// Commit the transaction based on the provided parameter and close the database connection
        /// </summary>
        public void CloseSession()
        {
            if (_dataContext != null)
            {
                _dataContext.Dispose();
            }
        }

        public void Dispose()
        {
            CloseSession();
        }

        /// <summary>
        /// Create an instance of RepositoryBase against the specific DataModel type and provide standard CRUD operation only.
        /// </summary>
        /// <typeparam name="T">The provided DataModel type.</typeparam>
        /// <returns>An instance of RepositoryBase against the specific DataModel type and provide standard CRUD operation only.</returns>
        public IRepository<T> CreateRepositoryBase<T>() where T : class
        {
            return new RepositoryBase<T>(_dataContext);
        }

        /// <summary>
        /// Create an instance of a class that implements IAnalysisResultDiagnosticMessageRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of class that implements IAnalysisResultDiagnosticMessageRepository.</returns>
        public IAnalysisResultDiagnosticMessageRepository CreateAnalysisResultDiagnosticMessageRepository()
        {
            return new AnalysisResultDiagnosticMessageRepository(_dataContext);
        }

        /// <summary>
        /// Create an instance of a class that implements IAnalysisItemRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of class that implements IAnalysisItemRepository.</returns>
        public IAnalysisItemRepository CreateAnalysisItemRepository()
        {
            return new AnalysisItemRepository(_dataContext);
        }

        /// <summary>
        /// Create an instance of a class that implements IAnalysisResultRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of class that implements IAnalysisResultRepository.</returns>
        public IAnalysisResultRepository CreateAnalysisResultRepository()
        {
            return new AnalysisResultRepository(_dataContext);
        }

        /// <summary>
        /// Create an instance of a class that implements IDiagnosticMessageReferenceRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of class that implements IDiagnosticMessageReferenceRepository.</returns>
        public IDiagnosticMessageReferenceRepository CreateDiagnosticMessageReferenceRepository()
        {
            return new DiagnosticMessageReferenceRepository(_dataContext);
        }

        /// <summary>
        /// Create an instance of a class that implements IBreastfeedingSignificanceRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of class that implements IBreastfeedingSignificanceRepository.</returns>
        public IBreastfeedingSignificanceRepository CreateBreastfeedingSignificanceRepository()
        {
            return new BreastfeedingSignificanceRepository(_dataContext);
        }

        /// <summary>
        /// Create an instance of a class that implements IPatientRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of class that implements IPatientRepository.</returns>
        public IPatientRepository CreatePatientRepository()
        {
            return new PatientRepository(_dataContext);
        }

        /// <summary>
        /// Create an instance of a class that implements IUnassignedSampleNumberRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of class that implements IUnassignedSampleNumberRepository.</returns>
        public IUnassignedSampleNumberRepository CreateUnassignedSampleNumberRepository()
        {
            return new UnassignedSampleNumberRepository(_dataContext);
        }
    }
}
